package com.example.hand.service.impl;

import com.example.hand.dataobject.CarBrand;
import com.example.hand.dto.BrandDTO;
import com.example.hand.repository.BrandRepository;
import com.example.hand.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandRepository brandRepository;

    @Override
    public CarBrand findById(Integer Id) {
        return brandRepository.findById(Id).get();

    }

    @Override
    public void deleteById(Integer Id) {
        brandRepository.deleteById(Id);
    }

    @Override
    public Page<CarBrand> findAll(Pageable pageable) {
        return brandRepository.findAllByOrderByBrandPriorityDesc(pageable);
    }

    @Override
    public CarBrand add(CarBrand carBrand) {
        if(carBrand.getBrandPriority() == null){
            carBrand.setBrandPriority(1);
        }
        return brandRepository.save(carBrand);
    }
}
